/*
 * Copyright &copy; 2011-2020 lnint Inc. All right reserved.
 * 
 * 修改信息：【与SVN提交信息一致】
 * A: 新增类 中文姓名 2014-12-17 12:02:25
 */
package com.lnint.jess.generator.entity;


import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.google.common.collect.Lists;
import com.lnint.jess.common.persistence.IdEntity;
import com.lnint.jess.common.utils.StringUtils;


/**
 * <P>表字段配置信息</P>
 * 
 * @version 1.0
 * @author 王磊 2015-03-19 12:02:25 11
 * @since JDK 1.6
 */
@Entity
@Table(name = "gen_scene_column")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class GenSceneColumn extends IdEntity<GenSceneColumn> {
	
	private static final long serialVersionUID = 1L;
	private GenScene genSceneId;	// 归属表
	private String name; 		// 列名
	private String comments;	// 描述
	private String jdbcType;	// JDBC类型
	private String javaType;	// JAVA类型
	private String javaField;	// JAVA字段名
	private String isPk;		// 是否主键（1：主键）
	private String isNull;		// 是否可为空（1：可为空；0：不为空）
	private String isEdit;		// 是否编辑字段（1：编辑字段）
	private String isEntity;	// 是否entity字段（1：entity字段）
	private String isForm;		// 是否表单字段（1：表单字段）
	private String isList;		// 是否列表字段（1：列表字段）
	private String isQuery;		// 是否查询字段（1：查询字段）
	private String isRelate;	// 级联关系（无，一对一，多对一）
	private String relateEntity;// 级联实体
	private String relateOp;	// 级联操作（0：无操作，1：级联删除，2：级联刷新，3：级联修改，4：级联新增，5：ALL）
	private String queryType;	// 是否模糊查询（1：模糊查询）
	private String showType;	// 字段生成方案（文本框、文本域、下拉框、复选框、单选框、字典选择、人员选择、部门选择、区域选择）
	private String dictType;	// 字典类型
	private Integer sort;		// 排序（升序）
	private String isSubtable; 	// 是否从表(0:主表， 1：从表)

	public GenSceneColumn() {
		super();
	}

	public GenSceneColumn(String id){
		super();
		this.id = id;
	}
	
    @JoinColumn(name = "gen_scene_id")
    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @NotNull
	public GenScene getGenSceneId() {
		return genSceneId;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param genSceneId the genSceneId to set
	 */
	public void setGenSceneId(GenScene genSceneId) {
		this.genSceneId = genSceneId;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the comments
	 */
	public String getComments() {
		return comments;
	}

	/**
	 * @param comments the comments to set
	 */
	public void setComments(String comments) {
		this.comments = comments;
	}

	/**
	 * @return the jdbcType
	 */
	public String getJdbcType() {
		return jdbcType;
	}

	/**
	 * @param jdbcType the jdbcType to set
	 */
	public void setJdbcType(String jdbcType) {
		this.jdbcType = jdbcType;
	}

	/**
	 * @return the javaType
	 */
	public String getJavaType() {
		return javaType;
	}

	/**
	 * @param javaType the javaType to set
	 */
	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}

	/**
	 * @return the javaField
	 */
	public String getJavaField() {
		return javaField;
	}

	/**
	 * @param javaField the javaField to set
	 */
	public void setJavaField(String javaField) {
		this.javaField = javaField;
	}

	/**
	 * @return the isPk
	 */
	public String getIsPk() {
		return isPk;
	}

	/**
	 * @param isPk the isPk to set
	 */
	public void setIsPk(String isPk) {
		this.isPk = isPk;
	}

	/**
	 * @return the isNull
	 */
	public String getIsNull() {
		return isNull;
	}

	/**
	 * @param isNull the isNull to set
	 */
	public void setIsNull(String isNull) {
		this.isNull = isNull;
	}
	/**
	 * @return the isEdit
	 */
	public String getIsEdit() {
		return isEdit;
	}

	/**
	 * @param isEdit the isEdit to set
	 */
	public void setIsEdit(String isEdit) {
		this.isEdit = isEdit;
	}

	/**
	 * @return the isList
	 */
	public String getIsList() {
		return isList;
	}

	/**
	 * @param isList the isList to set
	 */
	public void setIsList(String isList) {
		this.isList = isList;
	}

	/**
	 * @return the isQuery
	 */
	public String getIsQuery() {
		return isQuery;
	}

	/**
	 * @param isQuery the isQuery to set
	 */
	public void setIsQuery(String isQuery) {
		this.isQuery = isQuery;
	}

	/**
	 * @return the queryType
	 */
	public String getQueryType() {
		return queryType;
	}

	/**
	 * @param queryType the queryType to set
	 */
	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}

	/**
	 * @return the showType
	 */
	public String getShowType() {
		return showType;
	}

	/**
	 * @param showType the showType to set
	 */
	public void setShowType(String showType) {
		this.showType = showType;
	}

	/**
	 * @return the dictType
	 */
	public String getDictType() {
		return dictType;
	}

	/**
	 * @param dictType the dictType to set
	 */
	public void setDictType(String dictType) {
		this.dictType = dictType;
	}

	/**
	 * @return the sort
	 */
	public Integer getSort() {
		return sort;
	}

	/**
	 * @param sort the sort to set
	 */
	public void setSort(Integer sort) {
		this.sort = sort;
	}

	/**
	 * @return the isRelate
	 */
	public String getIsRelate() {
		return isRelate;
	}

	/**
	 * @param isRelate the isRelate to set
	 */
	public void setIsRelate(String isRelate) {
		this.isRelate = isRelate;
	}

	/**
	 * @return the relateEntity
	 */
	public String getRelateEntity() {
		return relateEntity;
	}

	/**
	 * @param relateEntity the relateEntity to set
	 */
	public void setRelateEntity(String relateEntity) {
		this.relateEntity = relateEntity;
	}

	/**
	 * @return the relateOp
	 */
	public String getRelateOp() {
		return relateOp;
	}

	/**
	 * @param relateOp the relateOp to set
	 */
	public void setRelateOp(String relateOp) {
		this.relateOp = relateOp;
	}

	/**
	 * @return the isForm
	 */
	public String getIsForm() {
		return isForm;
	}

	/**
	 * @param isForm the isForm to set
	 */
	public void setIsForm(String isForm) {
		this.isForm = isForm;
	}

	/**
	 * @return the isEntity
	 */
	public String getIsEntity() {
		return isEntity;
	}

	/**
	 * @param isEntity the isEntity to set
	 */
	public void setIsEntity(String isEntity) {
		this.isEntity = isEntity;
	}
	
	/**
	 * @return the isSubtable
	 */
	public String getIsSubtable() {
		return isSubtable;
	}

	/**
	 * @param isSubtable the isSubtable to set
	 */
	public void setIsSubtable(String isSubtable) {
		this.isSubtable = isSubtable;
	}
	
	/**
	 * 是否是基类字段
	 * @return
	 */
	@Transient
	public Boolean getIsNotBaseField(){
		return !StringUtils.equals(getName(), "id")
				&& !StringUtils.equals(getName(), "remarks")
				&& !StringUtils.equals(getName(), "create_by")
				&& !StringUtils.equals(getName(), "create_date")
				&& !StringUtils.equals(getName(), "update_by")
				&& !StringUtils.equals(getName(), "update_date")
				&& !StringUtils.equals(getName(), "del_flag");
	}
	/**
	 * 获取列注解列表
	 * @return
	 */
	@Transient
	public List<String> getAnnotationList(){
		List<String> list = Lists.newArrayList();
		// 导入Jackson注解
		if ("This".equals(getJavaType())){
			list.add("com.fasterxml.jackson.annotation.JsonBackReference");
		}
		if ("java.util.Date".equals(getJavaType())){
			list.add("com.fasterxml.jackson.annotation.JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")");
		}
		// 导入JSR303验证依赖包
		if (!"1".equals(getIsNull()) && !"String".equals(getJavaType())){
			list.add("javax.validation.constraints.NotNull(message=\""+getComments()+"不能为空\")");
		}
		return list;
	}
	
	/**
	 * 获取简写列注解列表
	 * @return
	 */
	@Transient
	public List<String> getSimpleAnnotationList(){
		List<String> list = Lists.newArrayList();
		for (String ann : getAnnotationList()){
			list.add(StringUtils.substringAfterLast(ann, "."));
		}
		return list;
	}
	/**
	 * 获取简写Java字段
	 * @return
	 */
	@Transient
	public String getSimpleJavaField(){
		return StringUtils.substringBefore(getJavaField(), ".");
	}
	
	/**
	 * 获取Java字段，如果是对象，则获取对象.附加属性1
	 * @return
	 */
	@Transient
	public String getJavaFieldId(){
		return StringUtils.substringBefore(getJavaField(), "|");
	}
	
	/**
	 * 获取Java字段，如果是对象，则获取对象.附加属性2
	 * @return
	 */
	@Transient
	public String getJavaFieldName(){
		String[][] ss = getJavaFieldAttrs();
		return ss.length>0 ? getSimpleJavaField()+"."+ss[0][0] : "";
	}
	
	/**
	 * 获取Java字段，所有属性名
	 * @return
	 */
	@Transient
	public String[][] getJavaFieldAttrs(){
		String[] ss = StringUtils.split(StringUtils.substringAfter(getJavaField(), "|"), "|");
		String[][] sss = new String[ss.length][2];
		if (ss!=null){
			for (int i=0; i<ss.length; i++){
				sss[i][0] = ss[i];
				sss[i][1] = StringUtils.toUnderScoreCase(ss[i]);
			}
		}
		return sss;
	}
}


